﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;

namespace ElingOA6_Admin_DSJCJ.Common
{
    public class OperateImg
    {
   /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="width">缩略图宽</param>
        /// <param name="height">缩略图高</param>
        /// <param name="imgPath">图片路径（相对）</param>
        /// <param name="isImgWater">图片是否加水印：=1加，=0不加</param>
        /// <returns></returns>
        public static byte[] breviaryImg(int width, int height, string strImgPath, int isImgWater)
        {
            string mode = "W";
            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(strImgPath));
            System.Drawing.Image bitmap;
            if (width != 0 && height != 0)
            {

                int towidth = width;
                int toheight = height;

                int x = 0;
                int y = 0;
                int ow = originalImage.Width;
                int oh = originalImage.Height;

                switch (mode)
                {
                    case "HW"://指定高宽缩放（可能变形）                
                        break;
                    case "W"://指定宽，高按比例                    
                        toheight = originalImage.Height * width / originalImage.Width;
                        break;
                    case "H"://指定高，宽按比例
                        towidth = originalImage.Width * height / originalImage.Height;
                        break;
                    case "Cut"://指定高宽裁减（不变形）                
                        if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                        {
                            oh = originalImage.Height;
                            ow = originalImage.Height * towidth / toheight;
                            y = 0;
                            x = (originalImage.Width - ow) / 2;
                        }
                        else
                        {
                            ow = originalImage.Width;
                            oh = originalImage.Width * height / towidth;
                            x = 0;
                            y = (originalImage.Height - oh) / 2;
                        }
                        break;
                    default:
                        break;
                }

                //新建一个bmp图片
                bitmap = new System.Drawing.Bitmap(towidth, toheight);

                //新建一个画板
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

                //设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //清空画布并以透明背景色填充

                //g.Clear(System.Drawing.Color.Transparent);

                g.Clear(System.Drawing.Color.White);

                //在指定位置并且按指定大小绘制原图片的指定部分
                g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
                    new System.Drawing.Rectangle(x, y, ow, oh),
                    System.Drawing.GraphicsUnit.Pixel);
                g.Dispose();
            }
            else
            {
                bitmap = originalImage;
            }

            if (isImgWater == 1)//添加水印
            {
                bitmap = addImgWater(bitmap);
            }


            //(bitmap as System.Drawing.Bitmap).MakeTransparent(Color.White);
            //将生成的图片发回客户端
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, ImageFormat.Jpeg);
            byte[] imgsNew = ms.ToArray();
            bitmap.Dispose();
            originalImage.Dispose();
            return imgsNew;
        }
        //图片加水印
        public static System.Drawing.Image addImgWater(System.Drawing.Image bitmap)
        {
            string strEntityNo = ConfigurationManager.AppSettings["EntityNo"].ToString();
            BLL.Entity entityBll = new BLL.Entity();
            Model.Entity entityObject = entityBll.GetModel(strEntityNo);
            if (entityObject != null)
            {
                if (entityObject.FWaterMarkCls == "1")//图片水印
                {
                    string strWaterImagePath = entityObject.FWaterMarkSvrFileName.Trim();
                    if (strWaterImagePath != "")
                    {
                        //string strImagePath = ConfigurationManager.AppSettings["imagePath"].ToString();//后台虚拟路劲名称
                        //string strNewWaterImagePath = "~/" + strImagePath + strWaterImagePath.Substring(1);
                        string strNewWaterImagePath = strWaterImagePath;
                        if (GenerateUtil.ExistsFile(strNewWaterImagePath))//缩略图存在
                        {
                            bitmap = setWater(bitmap, strNewWaterImagePath, 1);
                        }
                    }

                }
                else if (entityObject.FWaterMarkCls == "2")//文字水印
                {
                    string strWaterText = entityObject.FTxtWaterMark.Trim();
                    if (strWaterText != "")
                    {
                        bitmap = setWater(bitmap, strWaterText, 2);
                    }
                }
            }
            return bitmap;
        }
       //设置水印,zt=1水印图片路径，zt=2水印文字
        public static System.Drawing.Image setWater(System.Drawing.Image originalImage, string strWater, int zt)
        {
            if (zt == 1)
            {
                string Path_sypf = HttpContext.Current.Server.MapPath(strWater);//水印图片路劲
                System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(originalImage);

                int x = originalImage.Width - copyImage.Width;
                int y = originalImage.Height - copyImage.Height;

                g.DrawImage(copyImage, new System.Drawing.Rectangle(x, y, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
                g.Dispose();
                copyImage.Dispose();
            }
            else if (zt == 2)
            {
                Graphics g = Graphics.FromImage(originalImage);
                g.DrawImage(originalImage, 0, 0, originalImage.Width, originalImage.Height);

                Font f = new Font("黑体", 15, FontStyle.Bold, GraphicsUnit.Pixel);
                Brush b = new SolidBrush(Color.White);

                SizeF size = g.MeasureString(strWater, f);

                float textWidth = size.Width; //字符串像素宽度

                float textHeight = size.Height;//字符串像素高度

                float x = originalImage.Width - textWidth;
                float y = originalImage.Height - textHeight;

                g.DrawString(strWater, f, b, x, y);
                g.Dispose();

            }
            return originalImage;
        }
    }
}